/*
 * Author: yezi
 * E-mail: okye328@gmail.com
 * Create time: Apr 20, 2013 12:26:17 AM
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 *
 */

package net.mergebranch.view.resource;

import java.io.IOException;
import java.io.InputStream;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.context.ServletContextAware;

@Controller
public class RootResourceController implements ServletContextAware {

	private static final Logger LOGGER = LoggerFactory
			.getLogger(RootResourceController.class);

	private ServletContext servletContext;

	@Override
	public void setServletContext(ServletContext context) {
		this.servletContext = context;
	}

	public ServletContext getServletContext() {
		return this.servletContext;
	}

	@RequestMapping("/favicon.ico")
	public void getFavicon(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		response.setContentType("image/ico");
		InputStream in = getServletContext().getResourceAsStream(
				"WEB-INF/res/favicon.ico");
		IOUtils.copy(in, response.getOutputStream());
	}

	@RequestMapping("/robots.txt")
	public void getRobots(HttpServletRequest request,
			HttpServletResponse response) {
		LOGGER.info("Hello robots");
	}

}
